//
//  ContactMIDIController.m
//  MIDI
//
//  Created by 邵子凡 on 15/11/4.
//  Copyright © 2015年 QXSX. All rights reserved.
//

#import "ContactMIDIController.h"
#import "AddFeedbackBtnCell.h"
#import "AddDescriptionCell.h"
#import "AddImageProveCell.h"
#import "AddSendBtnCell.h"
#import "AppConfig.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "UIActionSheet+Blocks.h"
#import "MBProgressHUD+YC.h"
#import "UIViewController+NavItem.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "UserDataSingleton.h"
#import "CommFunc.h"

@interface ContactMIDIController ()<UITableViewDelegate,UITableViewDataSource,AddImageProveCellDelagate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,AddFeedbackBtnCellDelegate,AddSendBtnCellDelegate,QXNetWorkToolDelegate,AddDescriptionCellDelegate,UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) UIImagePickerController *peovePhoto;
@property (assign, nonatomic) NSInteger selectBtn;
@property (strong, nonatomic) UIImage *editedImage;
@property (strong, nonatomic) UIButton *feedbackBtn;
@property (strong, nonatomic) NSMutableDictionary * imageDict;
@property (strong, nonatomic) NSString *DescriptionText;
@property (strong, nonatomic) UIButton *addImageBtn;
@end

@implementation ContactMIDIController

- (NSMutableDictionary *)imageDict{
    
    if (!_imageDict) {
        
        NSDictionary *imageDict = @{@"image1":@"",@"image2":@"",@"image3":@""};
        
        _imageDict = [NSMutableDictionary dictionaryWithDictionary:imageDict];
    }
    
    return _imageDict;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //设置tableView样式
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //设置tableView背景颜色
    self.tableView.backgroundColor = UIColorRGBA(219, 219, 219, 1);
    
    //设置导航栏
    [self settingNavigation];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        return 187.5;
        
    }else if (indexPath.row == 1){
        
        return 172.5;
        
    }else if (indexPath.row == 2){
        
        return 177.5;
        
    }else{
        
        return 70.5;
        
    }
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        AddFeedbackBtnCell *cell = [AddFeedbackBtnCell cellWithTableView:tableView];
        
        cell.delegate = self;
        
        return cell;
        
    }else if (indexPath.row == 1){
        
        AddDescriptionCell *cell = [AddDescriptionCell cellWithTableView:tableView];
        
        cell.delegate = self;
        
        return cell;
        
    }else if (indexPath.row == 2){
        
        AddImageProveCell *cell = [AddImageProveCell cellWithTableView:tableView];
        
        cell.delegate = self;
        
        return cell;
        
    }else{
        
        AddSendBtnCell *cell = [AddSendBtnCell cellWithTableView:tableView];
        
        cell.delegate = self;
        
        return cell;
        
    }
    
    return nil;
}

- (void)settingNavigation{
    
    //设置导航栏标题
    self.navigationItem.title = @"联系我们";
    
    //设置导航栏标题字体颜色（字体大小也可以在这里设置）
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //设置导航栏右边按钮
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(sendBtn:)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    //设置导航栏左边返回按钮
    [self settingLeftBarButtonItem];
    
}

- (void)sendBtn:(UIBarButtonItem *)sender{
    
//    [self uploadAddSendBtn:(UIButton *)sender];
    
}

- (void)uploadImagePeove:(AddImageProveCell *)cell addImageBtn:(UIButton *)addImageBtn{
    
    self.selectBtn = addImageBtn.tag;
    self.addImageBtn = addImageBtn;
    [self uploadPeovePhoto];
}

- (void)uploadPeovePhoto{
    
    // UIImagePickerController 系统提供用来获取图片和视频的接口
    self.peovePhoto = [[UIImagePickerController alloc] init];
    self.peovePhoto.mediaTypes = @[(NSString *)kUTTypeImage];
    self.peovePhoto.allowsEditing = YES;
    self.peovePhoto.delegate = self;
    
    if ((YES)) {
        
        WEAKSELF
        [UIActionSheet showInView:self.view withTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"拍照",@"从相册选择"] tapBlock:^(UIActionSheet *actionSheet, NSInteger buttonIndex) {
            switch (buttonIndex) {
                case 0:
                    weakSelf.peovePhoto.sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:
                    weakSelf.peovePhoto.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                default:
                    break;
            }
            if (buttonIndex != actionSheet.cancelButtonIndex) {
                [weakSelf.navigationController presentViewController:weakSelf.peovePhoto animated:YES completion:nil];
            }
        }];
        
    }
    else{
        
        self.peovePhoto.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self.navigationController presentViewController:self.peovePhoto animated:YES completion:nil];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    if (picker == self.peovePhoto)
    {
        [picker dismissViewControllerAnimated:YES completion:^{
            
            self.editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
            
            switch (self.addImageBtn.tag) {
                case 0:
                    
                    [self.imageDict setValue:[CommFunc getImageBase64String:self.editedImage] forKey:@"image1"];
                    
                    break;
                    
                case 1:
                    
                    [self.imageDict setValue:[CommFunc getImageBase64String:self.editedImage] forKey:@"image2"];
                    
                    break;
                    
                case 2:
                    
                    [self.imageDict setValue:[CommFunc getImageBase64String:self.editedImage] forKey:@"image3"];
                    
                    break;
                    
                default:
                    break;
            }
            
            AddImageProveCell *cell =
            (AddImageProveCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
            
            if (self.selectBtn == 0) {
                
                [cell.addImage1 setImage:self.editedImage forState:UIControlStateNormal];
            }
            if (self.selectBtn == 1){
                
                [cell.addImage2 setImage:self.editedImage forState:UIControlStateNormal];
            }
            if (self.selectBtn == 2){
                
                [cell.addImage3 setImage:self.editedImage forState:UIControlStateNormal];
            }
        }];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

//第一个cell的Button的属性
- (void)uploadFeedbackBtn:(UIButton *)feedbackBtn{
    
    self.feedbackBtn = feedbackBtn;
}

//第二个cell的textView的属性
- (void)uploadAddDescription:(NSString *)DescriptionText{
    
    self.DescriptionText = DescriptionText;
}

//发送按钮上传资料实现
- (void)uploadAddSendBtn:(UIButton *)cell{
    
    [self toDownload];
    
}

- (void)uploadContacMIDI{
    
    if ([self.DescriptionText isEqualToString:@""] || !self.DescriptionText) {
        [MBProgressHUD showError:@"请填写描述问题"];
        
        return;
    }
    
    if (!self.editedImage) {
        [MBProgressHUD showError:@"请添加图片证明"];
        
        return;
    }
    
    [MBProgressHUD showMessage:@"正在加载"];
    
    QXNetWorkTool *QXnetWorkTool = [[QXNetWorkTool alloc]init];
    
    QXnetWorkTool.QXNetWorkDelegate = self;
    
    [QXnetWorkTool GetDataFromServerWithPostMethodandInterfaceName:ContactMIDIInterface andParameter:@[@{@"user_id":[UserDataSingleton shareInstance].user_id,@"call_type":[NSString stringWithFormat:@"%ld",(long)self.feedbackBtn.tag],@"call_content":self.DescriptionText,@"img1":_imageDict[@"image1"],@"img2":_imageDict[@"image2"],@"img3":_imageDict[@"image3"]}] tag:1];
}

/**
 *  上传失败
 */
- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    
    if (Response == nil)
        return;
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showError:Response];
            break;
    }
}

/**
 *  上传成功
 *
 *  @param message 是服务器返回上传成功的一个值
 *  @param tag     对应是哪个接口参数上传的标记
 */
- (void)GetSuccessMessage:(id)message andTag:(int)tag
{
    
    [MBProgressHUD hideHUD];
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showSuccess:message];
            
            break;
    }
}

//提示框 是否确定上传资料？
- (void)toDownload{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"上传资料" message:@"是否确定上传资料" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
    
    [alert addButtonWithTitle:@"确定"];
    
    [alert show];
    
}

//alertView 的代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        
        [self uploadContacMIDI];
        
    }
}


@end










